package com.example.testdemo.view

import android.content.Context
import android.graphics.Canvas
import android.graphics.Paint
import android.graphics.Rect
import android.util.AttributeSet
import android.widget.ImageView
import android.widget.RelativeLayout
import android.widget.TextView
import androidx.appcompat.widget.AppCompatTextView
import androidx.core.view.children
import java.lang.Exception

/**
 * Create Time:2020/9/1
 * Author: Lindanrong
 * Description:
 */
internal class MultiyTextImageView : RelativeLayout {

    lateinit var mTextPaint: Paint
    lateinit var mImagePaint: Paint
    var paintTextSize: Int = 5

    var reactTextLine: Rect = Rect() //获取到第一行的位置
    lateinit var reactDrawable: Rect //获取到第一行的位置


    constructor(context: Context?) : super(context) {

    }

    constructor(context: Context?, attrs: AttributeSet?) : super(context, attrs) {
        //获取传入属性
    }

    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec)
        //检测有没有多个view
        if (childCount == 2) {
            //计算控件的宽度跟高度
            var ivWidth = 0
            var ivHeight = 0
            if (getChildAt(0) is ImageView) {
                ivWidth = getChildAt(0).measuredWidth
                ivHeight = getChildAt(0).measuredHeight
            } else {
                throw Exception("第一个控件用于展示图像")
            }

            var tvWidth = 0
            var tvHeight = 0
            if (getChildAt(1) is TextView) {
                var tv = getChildAt(1) as TextView
                tvWidth = tv.measuredWidth
                tvHeight = tv.measuredHeight

                tv.layout.run {
                    var rect = Rect()
                    getLineBounds(0,rect)
                    rect.left = ivWidth

                }

            } else {
                throw Exception("第一个控件用于TextView")
            }

        } else {
            throw Exception("本控件只支持两个子控件")
        }

    }

    override fun onLayout(changed: Boolean, left: Int, top: Int, right: Int, bottom: Int) {

    }

    override fun onDraw(canvas: Canvas?) {
        super.onDraw(canvas)

    }


}